package com.cetc.stream;

import com.cetc.lambda.Person;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Classname StreamExptTest
 * @Description Stream表达式练习题
 * @Date 2023/1/25 23:02
 * @Created by louzhuangxi
 */
public class StreamExptTest {
    /**
     * 给定一个数字列表，如何返回一个由每个数的平方构成的列表呢？
     * 例如，给定【1，2，3，4，5】， 应该返回【1，4，9，16，25】
     */
    @Test
    public void test01() {
        List<Integer> iList = Arrays.asList(1, 2, 3, 4, 5);
        iList.stream().map(a -> a * a).collect(Collectors.toList()).forEach(System.out::println);
    }

    /**
     * 怎样用 map 和 reduce 方法数一数流中有多少个 Person 呢？
     */
    @Test
    public void test02() {
        List<Person> personList = Arrays.asList(
                new Person("张三", 24),
                new Person("李四", 80),
                new Person("王五", 28),
                new Person("赵六", 42),
                new Person("田七", 28)
        );
        Optional<Integer> reduce = personList.stream().map(t -> 1).reduce(Integer::sum);
        System.out.println(reduce.get());
    }

    /**
     * 1. 找出 2011 年发生的所有交易， 并按交易额排序（从低到高）
     * 2. 交易员都在哪些不同的城市工作过
     * 3. 查找所有来自剑桥的交易员，并按姓名排序
     * 4. 返回所有交易员的姓名字符串，按字母顺序排序
     * 5. 有没有交易员是在米兰工作的
     * 6. 打印生活在剑桥的交易员的所有交易额
     * 7. 所有交易中，最高的交易额是多少
     * 8. 找到交易额最小的交易
     */
    @Test
    public void test03() {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brain", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );
        //1. 找出 2011 年发生的所有交易， 并按交易额排序（从低到高）
        transactions.stream().filter(t -> t.getYear() == 2011).sorted(Comparator.comparing(Transaction::getValue)).forEach(System.out::println);

        //2. 交易员都在哪些不同的城市工作过
        transactions.stream().map(t -> t.getTrader().getCity()).collect(Collectors.toSet()).forEach(System.out::println);
        transactions.stream().map(t -> t.getTrader().getCity()).distinct().forEach(System.out::println);

        //3. 查找所有来自剑桥的交易员，并按姓名排序
        transactions.stream().map(Transaction::getTrader).filter(t -> "Cambridge".equals(t.getCity())).sorted(Comparator.comparing(Trader::getName)).distinct().
                forEach(System.out::println);

        //4. 返回所有交易员的姓名字符串，按字母顺序排序
        transactions.stream()
                .map(t -> t.getTrader().getName())
                .flatMap(StreamExptTest::filterCharacter)
                .sorted((t1, t2) -> t1.compareToIgnoreCase(t2))
                .forEach(System.out::print);
        System.out.println();


        //5. 有没有交易员是在米兰工作的
        boolean milan = transactions.stream().anyMatch(t -> "Milan".equals(t.getTrader().getCity()));
        System.out.println(milan);

        //6. 打印生活在剑桥的交易员的所有交易额
        int sum = transactions
                .stream()
                .filter(t -> "Cambridge".equals(t.getTrader().getCity()))
                .map(Transaction::getValue)
                .reduce(0, Integer::sum);
        System.out.println(sum);

        //7. 所有交易中，最高的交易额是多少
        Transaction maxTransaction = transactions.stream().max(Comparator.comparing(Transaction::getValue)).get();
        System.out.println(maxTransaction);

        //8. 找到交易额最小的交易
        Transaction minTransaction = transactions.stream().min(Comparator.comparing(Transaction::getValue)).get();
        System.out.println(minTransaction);
    }

    public static Stream<String> filterCharacter(String str) {
        List<String> list = new ArrayList<String>();
        for (Character ch : str.toCharArray()) {
            list.add(ch.toString());
        }
        return list.stream();
    }
}

/**
 * 交易员类
 */
class Trader {

    private String name;
    private String city;

    public Trader() {
        super();
    }

    public Trader(String name, String city) {
        super();
        this.name = name;
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Trader [name=" + name + ", city=" + city + "]";
    }
}


/**
 * 交易类
 */
class Transaction {

    private Trader trader;
    private int year;
    private int value;

    public Transaction() {
        super();
    }

    public Transaction(Trader trader, int year, int value) {
        super();
        this.trader = trader;
        this.year = year;
        this.value = value;
    }

    public Trader getTrader() {
        return trader;
    }

    public void setTrader(Trader trader) {
        this.trader = trader;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Transaction [trader=" + trader + ", year=" + year + ", value=" + value + "]";
    }

}



